/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.yc_cart;


import com.yuecai.modules.yc_cart.domain.YcCart;
import com.yuecai.modules.yc_cart.service.YcCartService;
import com.yuecai.modules.yc_cart.service.dto.YcCartDto;
import com.yuecai.modules.yc_cart.service.dto.YcCartQueryCriteria;
import com.yuecai.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
* @website https://el-admin.vip
* @author dm
* @date 2020-06-27
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "yc_cart管理")
@RequestMapping("/api/ycCart")
public class YcCartController {

    private final YcCartService ycCartService;


    @GetMapping
    @ApiOperation("查询yc_cart")
    public ResponseEntity<Object> query(YcCartQueryCriteria criteria, Pageable pageable){
        String username = SecurityUtils.getCurrentUsername();
        criteria.setUserId(Long.valueOf(username));
        return new ResponseEntity<>(ycCartService.queryAll(criteria,pageable),HttpStatus.OK);
    }


    @GetMapping("/sku")
    @ApiOperation("查询yc_cart")
    public ResponseEntity<Object> skuCount(Long skuId){
        HashMap map = ycCartService.skuCount(skuId);
        return  ResponseEntity.ok(map);
    }


    @PostMapping
    @ApiOperation("新增yc_cart")
    public ResponseEntity<Object> create(@RequestBody YcCart resources) throws Exception {
        try {
            YcCartDto ycCartDto = ycCartService.create(resources);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return new ResponseEntity<>("添加成功！",HttpStatus.OK);
    }

    @PutMapping
    @ApiOperation("修改yc_cart")
    public ResponseEntity<Object> update(@Validated @RequestBody YcCart resources){
        ycCartService.update(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/delete")
    @ApiOperation("删除yc_cart")
    public ResponseEntity<Object> deleteBySku(@RequestBody YcCart resources){
        ycCartService.delete(resources.getId());
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("删除yc_cart")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        ycCartService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}